package com.masterclient.app.modules.goods.adapter;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.bumptech.glide.load.Transformation;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.eoner.baselibrary.bean.goods.CommodityDetailData;
import com.eoner.baselibrary.bean.goods.SellerMessage;
import com.eoner.baselibrary.utils.ScreenUtils;
import com.eoner.baselibrary.utils.ViewTransformUtil;
import com.eoner.commonbean.product.ProductMessage;
import com.eoner.managerlibrary.router.AroutePath;
import com.masterclient.app.R;
import com.masterclient.app.base.MainViewHolder;

import java.util.List;

import jp.wasabeef.glide.transformations.RoundedCornersTransformation;

public class CommodityDetailSellerAdapter extends DelegateAdapter.Adapter<MainViewHolder> {

    Activity mContext;

    private LayoutHelper mLayoutHelper;
    private boolean isShowArrow = true;

    private VirtualLayoutManager.LayoutParams mLayoutParams;
    private int mCount = 0;

    CommodityDetailData data;


    public CommodityDetailSellerAdapter(Activity context, LayoutHelper layoutHelper, int count) {
        this(context, layoutHelper, count, null, null,true);
    }

    public CommodityDetailSellerAdapter(Activity context, LayoutHelper layoutHelper, int count,
                                        @NonNull VirtualLayoutManager.LayoutParams layoutParams, CommodityDetailData data,boolean isShowArrow) {
        this.mContext = context;
        this.mLayoutHelper = layoutHelper;
        this.mCount = count;
        this.mLayoutParams = layoutParams;
        this.data = data;
        this.isShowArrow = isShowArrow;
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return mLayoutHelper;
    }

    @Override
    protected void onBindViewHolderWithOffset(MainViewHolder holder, int position, int offsetTotal) {

    }

    @Override
    public MainViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return null;
    }

    @Override
    public void onBindViewHolder(MainViewHolder holder, int position) {
        if (mLayoutParams != null) {
            holder.itemView.setLayoutParams(
                    new VirtualLayoutManager.LayoutParams(mLayoutParams));
        }

        ImageView iv_shop_logo = holder.itemView.findViewById(R.id.iv_shop_logo);
        TextView tv_shop_name = holder.itemView.findViewById(R.id.tv_shop_name);
        TextView tv_main_busy = holder.itemView.findViewById(R.id.tv_main_busy);
        View rl_shop = holder.itemView.findViewById(R.id.rl_shop);
        View ll_recommend_products = holder.itemView.findViewById(R.id.ll_recommend_products);
        RecyclerView recommendRecycler = holder.itemView.findViewById(R.id.recommendRecycler);
        View ll_arrow = holder.itemView.findViewById(R.id.ll_arrow);
        View iv_dealer_icon = holder.itemView.findViewById(R.id.iv_dealer_icon);



        SellerMessage sellerMessage = data.getSeller();
        tv_shop_name.setText(sellerMessage.getStore_name());
        tv_main_busy.setText("主营：" + sellerMessage.getBusiness_scope());
        tv_main_busy.setVisibility(sellerMessage.isSeller_is_dealer()?View.GONE:View.VISIBLE);
        iv_dealer_icon.setVisibility(sellerMessage.isSeller_is_dealer()?View.VISIBLE:View.GONE);
        if (isShowArrow){
            ll_arrow.setVisibility(View.VISIBLE);
            rl_shop.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    ARouter.getInstance().build(AroutePath.STORE_ACTIVITY)
                            .withString("store_id",sellerMessage.getSeller_id())
                            .withString("is_dealer",sellerMessage.isSeller_is_dealer()?"1":"0")
                            .withString("is_agent", sellerMessage.isSeller_is_agent()?"1":"0").navigation();
                }
            });
        }else {
            ll_arrow.setVisibility(View.GONE);
        }
        ViewTransformUtil.glideImageView(mContext, sellerMessage.getLogo(), iv_shop_logo,
                new RoundedCornersTransformation(ScreenUtils.dp2px(7), 0), R.mipmap.defaults, ScreenUtils.dp2px(44), ScreenUtils.dp2px(44));
        LinearLayout storeTagRecommendll = holder.itemView.findViewById(R.id.store_tag_recommendll);
        ImageView storeTagRecommendicon = holder.itemView.findViewById(R.id.store_tag_recommendicon);
        TextView storeTagRecommend = holder.itemView.findViewById(R.id.store_tag_recommend);
        if (!sellerMessage.isSeller_is_dealer()&&sellerMessage.getShop_icon() != null && !TextUtils.isEmpty(sellerMessage.getShop_icon().getBg_header_color())) {
            storeTagRecommendll.setVisibility(View.VISIBLE);
            int[] colors = {Color.parseColor(sellerMessage.getShop_icon().getBg_header_color()),
                    Color.parseColor(sellerMessage.getShop_icon().getBg_footer_color())};
            GradientDrawable drawable = new GradientDrawable(GradientDrawable.Orientation.LEFT_RIGHT, colors);
            drawable.setCornerRadius(ScreenUtils.dp2px(2));
            drawable.setGradientType(GradientDrawable.LINEAR_GRADIENT);
            storeTagRecommendll.setBackground(drawable);
            storeTagRecommend.setText(sellerMessage.getShop_icon().getTitle());
            ViewTransformUtil.glideImageView(mContext, sellerMessage.getShop_icon().getPre_icon(), storeTagRecommendicon,
                    new Transformation[]{new CenterCrop()}, R.mipmap.defaults_1);
        }else {
            storeTagRecommendll.setVisibility(View.GONE);
        }
        if (!isShowArrow){
            storeTagRecommendll.setVisibility(View.GONE);
        }

        //店铺下面的推荐商品
        List<ProductMessage> productMessages = data.getSeller_recommend_products();
        if (productMessages != null && productMessages.size() > 0) {
            ll_recommend_products.setVisibility(View.VISIBLE);
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
            linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
            recommendRecycler.setLayoutManager(linearLayoutManager);
            //设置适配器
            SellerRecommendProductAdapter sellerRecommendProductAdapter = new SellerRecommendProductAdapter(mContext, data.getSeller_recommend_products(), data.getSeller().getSeller_id(),data.getSeller().getIs_boutique());
            recommendRecycler.setAdapter(sellerRecommendProductAdapter);
        } else {
            ll_recommend_products.setVisibility(View.GONE);
        }

    }

    @Override
    public int getItemCount() {
        return mCount;
    }

    @Override
    public int getItemViewType(int position) {
        return 5;
    }

}
